﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DaiChong.Lib.Http;
using System.Web;
using System.Text.RegularExpressions;

namespace WTest.Model
{
    public class Movie
    {
        public string siteName { get; set; }

        public int vod_id { get; set; }
        public int vod_cid { get; set; }
        public string vod_name { get; set; }
        public string vod_title { get; set; }
        public string vod_keywords { get; set; }
        public string vod_actor { get; set; }     //主演
        public string vod_director { get; set; }  //导演，主持人
        public string vod_content { get; set; }
        public string vod_pic { get; set; }
        public string vod_year { get; set; }
        public string vod_area { get; set; }
        public long vod_addtime { get; set; }
        public int vod_hits { get; set; }
        public int vod_stars { get; set; }
        public int vod_status { get; set; }
        public string vod_gold { get; set; }      //评分
        public string vod_letter { get; set; }    //首字母
        public string vod_url { get; set; }
        public string vod_play { get; set; }
        public int vod_continu { get; set; }         //更新到多少集

        public Movie(string _sitename)
        {
            siteName = _sitename;
            vod_hits = new Random(DateTime.Now.Millisecond).Next(100, 500);
            vod_stars = new Random(DateTime.Now.Millisecond).Next(1, 10);
            vod_status = 1;
            vod_play = "cmp";
            vod_addtime = GetTime(DateTime.Now);
            vod_year = DateTime.Now.Year.ToString();
            vod_area = "大陆";
            vod_continu = 0;
            vod_gold = "3";
        }

        public static long GetTime(string dateStr)
        {
            DateTime dt = Convert.ToDateTime(dateStr);
            return (dt.Ticks - DateTime.Parse("1970-01-01 00:00:00").Ticks) / 10000000;
        }

        public static long GetTime(DateTime dt)
        {
            return (dt.Ticks - DateTime.Parse("1970-01-01 00:00:00").Ticks) / 10000000;
        }

        public static long GetMillTime(DateTime dt)
        {
            return (dt.Ticks - DateTime.Parse("1970-01-01 00:00:00").Ticks) / 10000;
        }

        public bool Exist(string vod_name)
        {
            MySqlHelper sqlHelper = new MySqlHelper(siteName);
            string sql = string.Format("select count(vod_id) from ff_vod where vod_name='{0}'", vod_name.Trim());
            object a = sqlHelper.ExecuteScale(CommandType.Text, sql, null);
            if (a != null && Convert.ToInt32(a.ToString()) > 0)
                return true;
            return false;
        }

        public bool Exist(string vod_name, out int vod_id, out int vod_continu)
        {
            MySqlHelper sqlHelper = new MySqlHelper(siteName);
            bool bExist = false;
            vod_id = 0;
            vod_continu = 0;
            string sql = string.Format("select vod_id, vod_continu from ff_vod where vod_name='{0}'", vod_name.Trim());

            DataTable dt = sqlHelper.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                bExist = true;
                vod_id = Convert.ToInt32(dr["vod_id"].ToString());
                vod_continu = Convert.ToInt32(dr["vod_continu"].ToString());
            }
            
            return bExist;
        }

        public int Insert(Movie m)
        {
            MySqlHelper sqlHelper = new MySqlHelper(siteName);

            string strSql = string.Format("insert into ff_vod(vod_cid,vod_name,vod_title,vod_keywords,vod_actor,vod_director,vod_content,vod_pic,vod_year,vod_area,vod_addtime,vod_continu,vod_hits,vod_stars,vod_status,vod_gold,vod_url,vod_play)" +
                "values({0},'{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',{11},'{12}','{13}','{14}','{15}','{16}','{17}')",
                m.vod_cid,
                m.vod_name,
                m.vod_title,
                m.vod_keywords,
                m.vod_actor,
                m.vod_director,
                m.vod_content,
                m.vod_pic,
                m.vod_year,
                m.vod_area,
                m.vod_addtime,
                m.vod_continu,
                m.vod_hits,
                m.vod_stars,
                m.vod_status,
                m.vod_gold,
                m.vod_url,
                m.vod_play);
            object a = sqlHelper.ExecuteNonQuery(CommandType.Text, strSql, null);
            if (a != null)
            {
                return Convert.ToInt32(a.ToString());
            }
            else
                return 0;
        }

        public int UpdateUrl(Movie m)
        {
            MySqlHelper sqlHelper = new MySqlHelper(siteName);

            string strSql = string.Format("update ff_vod set vod_continu = {0}, vod_url = concat(vod_url, '\r\n', '{1}') where vod_id={2}",
                m.vod_continu,
                m.vod_url,
                m.vod_id);
            object a = sqlHelper.ExecuteNonQuery(CommandType.Text, strSql, null);
            if (a != null)
            {
                return Convert.ToInt32(a.ToString());
            }
            else
                return 0;
        }

        public int ReplaceUrl(Movie m)
        {
            MySqlHelper sqlHelper = new MySqlHelper(siteName);

            string strSql = string.Format("update ff_vod set vod_continu = {0}, vod_url = '{1}') where vod_id={2}",
                m.vod_continu,
                m.vod_url,
                m.vod_id);
            object a = sqlHelper.ExecuteNonQuery(CommandType.Text, strSql, null);
            if (a != null)
            {
                return Convert.ToInt32(a.ToString());
            }
            else
                return 0;
        }

        public int UpdateStarAndHits(Movie m)
        {
            MySqlHelper sqlHelper = new MySqlHelper(siteName);

            string strSql = string.Format("update ff_vod set vod_stars={0} ,vod_hits={1} where vod_id={2}",
                m.vod_stars,
                m.vod_hits,

                m.vod_id);
            object a = sqlHelper.ExecuteNonQuery(CommandType.Text, strSql, null);
            if (a != null)
            {
                return Convert.ToInt32(a.ToString());
            }
            else
                return 0;
        }

        public static string GetFlv(string playurl)
        {
            string submitUrl = string.Format("http://www.flvcd.com/parse.php?flag=&format=high&kw={0}&sbt=%BF%AA%CA%BCGO%21", HttpUtility.UrlEncode(playurl));
            submitUrl = string.Format("http://www.flvcd.com/parse.php?flag=one&format=high&kw={0}&sbt=%BF%AA%CA%BCGO%21", HttpUtility.UrlEncode(playurl));
            Submit web = new Submit() { 
                Encode = Encoding.GetEncoding("gb2312"),
                URL = submitUrl,
                Referer = "http://www.flvcd.com/"
            };

            web.Get();
            Match mch = Regex.Match(web.Html, @"下载地址：[^""]+?""([^\?]+?)\?");
            if (mch.Success)
                return mch.Groups[1].Value + "?start={start_bytes}";
            
            return "";
        }

    }
}
